<template>
	<view>
		<uni-drawer ref="fliter" mode="right">
			<view class="filter">
				<view class="filter-line">
					<view class="filter-title">{{$t('equipment.common.locaNo')}}</view>
					<view class="filter-input">
						<input :placeholder="$t('baseCommon.message.pleaseEnter', { title: $t('equipment.common.locaNo') })" v-model="locaNo" />
					</view>
				</view>
				<view class="filter-line">
					<view class="filter-title">{{$t('equipment.common.locaName')}}</view>
					<view class="filter-input">
						<input :placeholder="$t('baseCommon.message.pleaseEnter', { title: $t('equipment.common.locaName') })" v-model="locaName" />
					</view>
				</view>
				<view class="filter-btn">
					<button type="primary" @click="doQuery">{{$t('baseCommon.buttons.confirm')}}</button>
					<button type="default" @click="doClear">{{$t('baseCommon.buttons.reset')}}</button>
				</view>
			</view>
		</uni-drawer>

		<view v-if="showEq">
			<equipment-list ref="mescrollItem" :equipment="equipment" @getAsset="getAsset"></equipment-list>
		</view>

		<view class="select-btn">
			<button type="primary" @click="doSure">{{$t('baseCommon.buttons.confirm')}}</button>
		</view>
	</view>
</template>

<script>
	import equipmentList from '@/components/base/equipment-list/equipment-list.vue'
	import MescrollCompMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-comp.js"
	import { getTopTreeList } from '@/api/base/equipment.js'

	export default {
		mixins: [MescrollCompMixin],
		components: {
			equipmentList
		},
		data() {
			return {
				maintainDeptNo: '',
				assetIsNull: null,
				showEq: false,
				equipment: {},
				locaNo: '',
				locaName: '',
				locaList: [],
				checkedItem: {}
			}
		},
		onLoad(option) {
			this.maintainDeptNo = option.maintainDeptNo ? option.maintainDeptNo : ""
			this.assetIsNull = option.assetIsNull ? !!option.assetIsNull : null
			this.init()
		},
		methods: {
			async init() {
				await this.initEquipLocationData().then(res => {
					res.forEach(item => {
						this.locaList.push({
							id: item.key,
							label: item.title,
							pathData: item.data,
							children: item.children
						})
					})
				})

				this.equipment = {
					"tree": this.locaList,
					"locaId": "default",
					"path": "",
					"initParams": {
						"maintainDeptNo": this.maintainDeptNo,
						"assetIsNull": this.assetIsNull
					}
				}
				this.showEq = true
			},
			async initEquipLocationData(locaId) {
				let list = []
				// await getTopTreeList(uni.getStorageSync('companyId')).then(res => {
				await getTopTreeList('').then(res => {
					list = res.data
				})
				return list
			},
			doQuery() {
				this.$refs.fliter.close()
				const fliteForm = {
					fliterLocaNo: this.locaNo,
					fliterLocaName: this.locaName
				}
				this.$refs.mescrollItem.listFilter(fliteForm)
			},
			doClear() {
				this.locaNo = ""
				this.locaName = ""
			},
			getAsset(data) {
				this.checkedItem = data
			},
			doSure() {
				const eventChannel = this.getOpenerEventChannel();
				eventChannel.emit('selectLoca', this.checkedItem);
				uni.navigateBack();
			}
		},
		onNavigationBarButtonTap(e) {
			this.$refs.fliter.open()
		}
	}
</script>

<style lang="scss">
	@import '@/static/style/common-list.scss';
</style>
